define(function () {
    'use strict';
    function ctrl($scope, $ionicLoading, $timeout, LocalCacheService, CommonService, WebService, WTService) {
        var timer;
        var seconds;

        function upadateSeconds() {
            if (seconds == 0) {
                $scope.buttonState.checkState = false;
                $scope.buttonState.checkText = '获取验证码';
                seconds = 180;
                return;
            }
            seconds--;
            $scope.buttonState.checkText = seconds + '秒后失效';
            timer = $timeout(function () {
                upadateSeconds();
            }, 1000);
        }

        function init() {
            if(timer){
                $timeout.cancel(timer);
            }
            seconds = 180;
            $scope.mobileNew = {
                mobile: '',
                vCode: ''
            };
            $scope.buttonState = {
                checkState: false,
                submitState: false,
                checkText: '获取验证码'
            };
            $scope.formEvents = {
                getCode: function () {
                    $scope.buttonState.checkState = true;
                    var params = {mobile_tel: $scope.mobileNew.mobile,type:'2',app_id:'gcszt'};
                    WTService.sendMobileCode(params).then(function () {
                            upadateSeconds();
                        },
                        function (result) {
                            $scope.buttonState.checkState = false;
                            var message=CommonService.getErrorInfo(result);
                            if (message=='参数传入错误'){
                                message='请输入正确的手机号码';
                            }
                            CommonService.showAlert({message: message});
                        }
                    );
                },
                submit: function () {
                    $ionicLoading.show();
                    var params1 = {mobile_tel: $scope.mobileNew.mobile, mobile_code: $scope.mobileNew.vCode,app_id:'gcszt'};
                    WTService.checkMobileCode(params1).then(
                        function (data) {
                            $ionicLoading.hide();
                            if (data.error_info) {
                                CommonService.showAlert({message: data.error_info});
                            } else {
                                var params2 = {new_mobile_tel:params1.mobile_tel,app_id:'gcszt'};
                                WTService.modifyMobile(params2).then(
                                    function (data) {
                                        $ionicLoading.hide();
                                        if (data.error_info) {
                                            CommonService.showAlert({message: data.error_info});
                                        } else {
                                            var user = LocalCacheService.getUser();
                                            user.mobile_tel = params1.mobile_tel;
                                            LocalCacheService.setUser(user);
                                            CommonService.showComplete('../view/template/phonetemplate.html', '返回', 'tab.account-manager');
                                        }
                                    },
                                    function () {
                                        $ionicLoading.hide();
                                        CommonService.showAlert({message: '手机号修改失败'});
                                    }
                                );
                            }
                        },
                        function (result) {
                            $ionicLoading.hide();
                            var message=CommonService.getErrorInfo(result);
                            switch (message){
                                case '参数传入错误' : message='请输入正确的手机号码及验证码';
                                    break;
                                case '未知错误' : message='验证码错误';
                                    break;
                            }
                            CommonService.showAlert({message: message});
                        }
                    );
                }
            }
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });
    }

    ctrl.$inject = ['$scope', '$ionicLoading', '$timeout', 'LocalCacheService', 'CommonService', 'WebService', 'WTService'];
    return ctrl;
});